<!-- 所有公司 -->

<template>
  <div class="resume_header">
    <span>所有用户</span>
  </div>
  <div class="resume_center">
    <el-table :data="filterTableData.slice((page - 1) * limit,page*limit)" style="width: 100%" height="400px"
      size="large">
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="性别" prop="gander" />
      <el-table-column label="手机号" prop="phone" />
      <el-table-column label="邮箱" prop="email" />
      <el-table-column label="所在城市" prop="address" />
      <el-table-column label="学历" prop="education" />
      <el-table-column label="院校" prop="schoolName" />
      <el-table-column align="right">
        <template #header>
          <el-input v-model="search" size="default" placeholder="搜索" />
        </template>
        <template #default="scope">
          <el-button size="default" type="primary" @click="handleInitPwd(scope.$index, scope.row)">初始化密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <Pagination :page="page" :limit="limit" :handleSizeChange="handleSizeChange"
        :handleCurrentChange="handleCurrentChange" :filterTableData="filterTableData" :pagesizes="pagesizes" />
    </div>
  </div>

</template>
<script lang='ts' setup>
import { template } from 'lodash';
import { computed, ref, onMounted, reactive, onBeforeMount } from 'vue'
import { useStore } from "vuex"
import Pagination from "@/components/Pagination.vue"
import {adminGetAllUsers,adminInitPwd} from "@/api/adminApi"
import { data,elmessage } from '@/hook/hook';
import {useRouter} from "vue-router"
const store = useStore()
const router=useRouter()
const users = store.state.uState.users
// 表格数据定义
interface Users {
  uId: string,
  name: string,
  gander: string,
  phone: string,
  email: string,
  address: string,
  education: string,
  schoolName: string,
}
const tableData: Users[] = reactive([])

const search = ref('')
const filterTableData = computed(() =>

  tableData.filter(
    (data, index) =>
      !search.value ||
      // 通过岗位名称筛选
      data.name.toLowerCase().indexOf(search.value.toLowerCase()) !== -1
  )

)


// 分页器
const page = ref(1)
const pagesizes = ref([1, 2, 3, 4, 10, 20, 30, 40])
const limit = ref(10)

const handleSizeChange = (val: number) => {
  limit.value = val
  page.value = 1

}
const handleCurrentChange = (val: number) => {
  page.value = val
}


// 初始化数据
function initData() {
  users.forEach((value: any, index: any, self: any) => {
    tableData.push(value)
  })
}
const  handleInitPwd=(index: number, row: Users)=>{
  const newData={
    ...data(),
    initPart:"user",
    uId:row.uId
  }
  adminInitPwd(newData).then((res:any)=>{
    if (res.data["code"] == "200") {
      elmessage(res.data["message"], res.data["type"])
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data);

    }
  })
}

const init=()=>{
  adminGetAllUsers(data()).then((res:any)=>{
    if (res.data["code"] == "200") {
      res.data.datas.forEach((value: any, index: any, self: any) => {
        tableData.push(value)
      })
    } else if (res.data["code"] == "300") {
      elmessage(res.data["message"], res.data["type"])
      setTimeout(() => {
        router.replace("/login")
      }, 500);
      localStorage.clear()
    } else {
      console.log(res.data);

    }
  })
}

onBeforeMount(()=>{
  init()
})




</script>
<style scoped>
.pagination {
  float: right;
  margin-right: 50px;
  /* right: 150px; */
}

.resume_center {
  font-family: '微软雅黑';
  height: 800px;
  background-color: #FFF;
}

.resume_header {
  background-color: rgb(215, 215, 215);
  height: 50px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  justify-content: space-between;
}

a {
  text-decoration: none;
  color: #000;
}
</style>